package datebase.live.access;

import java.sql.Date;

import models.TrackDaysModel;
import models.TrackModel;
import models.TrackPicturesModel;
import models.UserTrackDaysModel;

import com.trackvoodoo.models.DbPopulateDocument.DbPopulate;
import com.trackvoodoo.models.RaceTracksDocument.RaceTracks;
import com.trackvoodoo.models.TrackDaysDocument.TrackDays;
import com.trackvoodoo.models.TrackDocument.Track;
import com.trackvoodoo.models.TrackPicturesDocument.TrackPictures;
import com.trackvoodoo.models.UserTrackDaysDocument.UserTrackDays;

import database.connector.DatabaseFactory;
import database.live.DOAStatements;
import database.live.MySqlDAO;

public class TrackDAO {

	/*
	 * dataDay objects.. and what it needs to get.
	 */
	
	private TrackDAO(){};
	
	//Track
	public int addTrack(String name, String description)
	{
		DbPopulate db = DbPopulate.Factory.newInstance();
		MySqlDAO.addDBElement(db, 1, name);
		MySqlDAO.addDBElement(db, 2, description);
		
		return MySqlDAO.parseAndUpdateInsertDelete(DOAStatements.ADD_TRACK,db);
		
	}
	
	public static int deleteTrack(int trackId)
	{
		DbPopulate db = DbPopulate.Factory.newInstance();
		MySqlDAO.addDBElement(db, 1, trackId);
		
		return MySqlDAO.parseAndUpdateInsertDelete(DOAStatements.DELETE_TRACK,db);
	}

	public static Track getTrackByName(String name)
	{
		RaceTracks r= new TrackModel().getTracks();
		Track[] trackArray = r.getTrackArray();
		for (Track t : trackArray)
		{
			if (t.getName().equalsIgnoreCase(name))
			{
				return t;
			}
		}
		return null;
	}
	
	public static Track getTrackById(int id)
	{
		RaceTracks r= new TrackModel().getTracks();
		Track[] trackArray = r.getTrackArray();
		for (Track t : trackArray)
		{
			if (t.getId() == id)
			{
				return t;
			}
		}
		return null;
	}
	
	//TrackDate
	public static int addTrackDate(int trackRef,Date date, String description )
	{
		DbPopulate db = DbPopulate.Factory.newInstance();
		MySqlDAO.addDBElement(db, 1, trackRef);
		MySqlDAO.addDBElement(db, 2, date);
		MySqlDAO.addDBElement(db, 3, description);
		
		return MySqlDAO.parseAndUpdateInsertDelete(DOAStatements.ADD_TRACKDATE,db);//trackRef, date, decription
	}
	
	public static int deleteTrackDate(int idTrackDays)
	{
		DbPopulate db = DbPopulate.Factory.newInstance();
		MySqlDAO.addDBElement(db, 1, idTrackDays);
		
		return MySqlDAO.parseAndUpdateInsertDelete(DOAStatements.DELETE_TRACKDATE,db);
	}
	
	public static TrackDays getTrackDates()
	{
		return new TrackDaysModel().getTrackDays();
	}
	
	//TrackPics
	public static int addTrackPicture(int trackRef, String trackOverview, String location, String picture)
	{
		DbPopulate db = DbPopulate.Factory.newInstance();
		MySqlDAO.addDBElement(db, 1, trackRef);
		MySqlDAO.addDBElement(db, 2, trackOverview);
		MySqlDAO.addDBElement(db, 3, location);
		MySqlDAO.addDBElement(db, 4, picture);
		
		return MySqlDAO.parseAndUpdateInsertDelete(DOAStatements.ADD_TRACK_PICTURES,db); //trackRef, trackOverview, location, picture
	}
	
	public static int deleteTrackPicture(int idPictures)
	{
		DbPopulate db = DbPopulate.Factory.newInstance();
		MySqlDAO.addDBElement(db, 1, idPictures);
		
		return MySqlDAO.parseAndUpdateInsertDelete(DOAStatements.DELETE_TRACK_PICTURES,db);
	}
	
	public static TrackPictures getTrackPictures()
	{
		return new TrackPicturesModel().getTrackPictures();
	}
	
	//Traks
	public static RaceTracks getTracks()
	{
		return new TrackModel().getTracks();
	}
	
	//User TrackDays
	
	public static int addUserTrackDay(int trackRef, Date date, int userRef, boolean complete, int carRef)
	{
		DbPopulate db = DbPopulate.Factory.newInstance();
		MySqlDAO.addDBElement(db, 1, trackRef);
		MySqlDAO.addDBElement(db, 1, date);
		MySqlDAO.addDBElement(db, 1, userRef);
		MySqlDAO.addDBElement(db, 1, complete);
		MySqlDAO.addDBElement(db, 1, carRef);
		return MySqlDAO.parseAndUpdateInsertDelete(DOAStatements.ADD_USERTRACKDATE, db);
	}
	
	public static int deleteUserTrackDay(int isUserTrackDay)
	{
		DbPopulate db = DbPopulate.Factory.newInstance();
		MySqlDAO.addDBElement(db, 1, isUserTrackDay);
		return MySqlDAO.parseAndUpdateInsertDelete(DOAStatements.DELETE_USERTRACKDATE, db);
	}
	
	public static UserTrackDays getUserTrackDays()
	{
		return new UserTrackDaysModel().getUserTrackDays();
	}
}
